import axios from 'axios';

// 创建axios实例
const api = axios.create({
  baseURL: '/api', // 根据实际情况配置基础URL
  timeout: 10000, // 请求超时时间
});

// 请求拦截器
api.interceptors.request.use(
  (config) => {
    // 可以在这里添加token等认证信息
    const token = localStorage.getItem('token');
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
api.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 用户相关API
export const userApi = {
  login: (data) => api.post('/user/login', data),
  register: (data) => api.post('/user/register', data),
  getUserInfo: () => api.get('/user/info'),
  getUserList: (params) => api.get('/user/list', { params }),
  updateUser: (data) => api.put(`/user/${data.userid}`, data),
  deleteUser: (id) => api.delete(`/user/${id}`),
};

// 设备相关API
export const equipmentApi = {
  getEquipmentList: (params) => api.get('/equipment/list', { params }),
  getEquipmentById: (id) => api.get(`/equipment/${id}`),
  addEquipment: (data) => api.post('/equipment', data),
  updateEquipment: (data) => api.put(`/equipment/${data.equipment_id}`, data),
  deleteEquipment: (id) => api.delete(`/equipment/${id}`),
};

// 命令相关API
export const commandApi = {
  getCommandList: (params) => api.get('/command/list', { params }),
  getCommandById: (id) => api.get(`/command/${id}`),
  addCommand: (data) => api.post('/command', data),
  updateCommand: (data) => api.put(`/command/${data.command_id}`, data),
  deleteCommand: (id) => api.delete(`/command/${id}`),
};

// 公司相关API
export const companyApi = {
  getCompanyList: (params) => api.get('/company/list', { params }),
  getCompanyById: (id) => api.get(`/company/${id}`),
  addCompany: (data) => api.post('/company', data),
  updateCompany: (data) => api.put(`/company/${data.company_id}`, data),
  deleteCompany: (id) => api.delete(`/company/${id}`),
};

// 协议相关API
export const agreementApi = {
  getAgreementList: (params) => api.get('/agreement/list', { params }),
  getAgreementById: (id) => api.get(`/agreement/${id}`),
  addAgreement: (data) => api.post('/agreement', data),
  updateAgreement: (data) => api.put(`/agreement/${data.agreement_id}`, data),
  deleteAgreement: (id) => api.delete(`/agreement/${id}`),
};

export default api; 